<?php

namespace app\logic;

use app\model\Account;

class UserLogic
{
    public static function login($data)
    {
        if ($account = Account::where('mobile', $data['mobile'])->where('df', 0)->field('id as uid,pwd')->cache(60)->find()) {
            if (password_verify($data['pwd'], $account->pwd)) {
                unset($account->pwd);
                $account->time = time();
                $account->uniqid = uniqid($account->uid);
                $token = \xy_jx\Utils\Encryption::encrypt($account->toArray());
                $redis = \Utils::redis();
                $redis->set('Account:token_' . $account->uid, $token, 86400 * 7);
                return $token;
            }
        }
        return false;
    }

    public static function newToken($user)
    {
        $user['time'] = time();
        $user['uniqid'] = uniqid($user['uid']);
        if ($token = \xy_jx\Utils\Encryption::encrypt($user)) {
            $redis = \Utils::redis();
            $redis->set('Account:token_' . $user['uid'], $token, 86400 * 7);
            return $token;
        }
        return false;
    }
}